/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ourMessage;

import ChateoController.Controller;
import ChateoFacadeNI.FacadeNI;
import java.io.IOException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author olga
 * @author adrian
 */
public class ConnexionUDPServer extends Thread {

    private DatagramSocket socketServ;
    private FacadeNI facada;
    private final int DATA_SIZE = 1000;
    /**
     * /As the variable is static we CAN'T create multiple servers from one
     * thread main thread.
     */
    private static int portServ = 2014;

    public ConnexionUDPServer(int portServ, FacadeNI facada) {
        ConnexionUDPServer.portServ = portServ;
        this.facada = facada;
        //Create a UDP socket linked to the server's port
        System.out.println("In " + ConnexionUDPServer.class.getName() + " : Creating the server socket on " + portServ);
        try {
            this.socketServ = new DatagramSocket(portServ);
        } catch (SocketException ex) {
            System.out.println("Probleme lors de la creation du Serveur");
            Logger.getLogger(ConnexionUDPServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        /**
         * /Starting the reception Thread
         */
        super.start();
    }

    @Override
    public void run() {
        boolean active = true;
        String currentLogin;
        if (Controller.DebugMode()) {
            System.out.println("Server Thread Started");
        }
        while (active) {
            try {
                /**
                 * /An array for data reception
                 */
                byte[] data = new byte[DATA_SIZE];

                /**
                 * /Creation of a packet using the data array
                 */
                DatagramPacket packet = new DatagramPacket(data, data.length);

                //Waiting for a packet
                socketServ.receive(packet);
                if (Controller.DebugMode()) {
                    System.out.println("A Packet has been received");
                }

                /**
                 * /Getting IP Adress of the current client
                 */
                InetAddress ClientIP = packet.getAddress();

                /**
                 *
                 */
                Message msg = Message.fromArray(data);

                if (msg instanceof Hello) {

                    Hello hello = (Hello) msg;
                    currentLogin = hello.getlogin();
                    if (hello.isAck()) {
                        facada.incommingAck(currentLogin, ClientIP);
                    } else {
                        facada.incommingHello(currentLogin, ClientIP);
                    }
                } else if (msg instanceof Bye) {
                    Bye bye = (Bye) msg;
                    currentLogin = bye.getLogin();
                    facada.incommingBye(currentLogin, ClientIP);

                } else if (msg instanceof Text) {
                    Text text = (Text) msg;
                    currentLogin = text.getlogin();
                    facada.incommingText(ClientIP, currentLogin, text.getText(), text.isPrivate());

                } else {
                    //WHAT IS THIS MESSAGE ?!
                    System.out.println("ERROR : What is this message ?");
                }

                System.out.println("The server has received : " + msg.toString());
            } catch (IOException e) {
                active = false;
            } catch (ClassNotFoundException e) {
                active = false;
            }
        }

    }

}
